package com.message.tcp.ip.socket.builder.testcase;

import org.apache.log4j.Logger;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.packager.GenericPackager;
import org.springframework.stereotype.Service;

import com.message.tcp.ip.socket.util.MessageDefinition;
import com.message.tcp.ip.socket.util.UtilPackage;

@Service("networkSignOn")
public class NetworkMessageVerifyImp implements NetworkMessageVerify {
	private static final Logger logger = Logger.getLogger(NetworkMessageVerifyImp.class);
	private static final String REQ_MTI = "0800";
	//private static final String RES_MTI = "0810";
	private GenericPackager packager = MessageDefinition.getGenericPackager();
	@Override
	public byte[] signOnMessageBuilder() {
		try {
			return buildNetworkMessage(MessageDefinition.SIGNON);
		} catch (ISOException e) {
			logger.debug("Exception occured while try to build sign on message", e);
			return null;
		}
	}

	@Override
	public boolean signOnResValidator(byte[] data) {
		ISOMsg isoMsg = new ISOMsg();
		isoMsg.setPackager(packager);
		byte[] raw = UtilPackage.extractMessage(data);
		try {
			isoMsg.unpack(raw);
		} catch (ISOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public byte[] signOffMessageBuilder() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean signOffResValidator(byte[] data) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public byte[] echoMessageBuilder() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean echoMessageValidator(byte[] data) {
		// TODO Auto-generated method stub
		return false;
	}
	
	private byte[] buildNetworkMessage(String type) throws ISOException{
		String mti = REQ_MTI;
		String field07 = MessageDefinition.buildField07();
		String field11 = MessageDefinition.buildField11();
		String field37 = MessageDefinition.buildField37(field11);
		String field70 = type;
		ISOMsg isoMsg = new ISOMsg();
		isoMsg.setPackager(packager);
		isoMsg.setMTI(mti);
		isoMsg.set( 7, field07);
		isoMsg.set(11, field11);
		isoMsg.set(37, field37);
		isoMsg.set(70, field70);
		byte[] pack=isoMsg.pack();
		System.out.println(new String(pack));
		return UtilPackage.addHeader(pack);
	}
}
